package tech.aistar.controller;

import tech.aistar.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author: lya
 * @Description: 本类的作用描述:
 * @CreateDate: 2019/9/6
 * @Version: 1.0
 */
@WebServlet(urlPatterns = "/nav/*")
public class NavAction extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //设置文档类型
        resp.setContentType("text/html");
        //设置编码
        resp.setCharacterEncoding("UTF-8");
        //获取文档输出流
        PrintWriter out = resp.getWriter();

        User user = (User) req.getSession().getAttribute("user");

        out.println("<!DOCTYPE html>");
        out.println("<html lang='en'>");

        out.println("<head>");
        out.println("<meta charset='UTF-8'>");
        out.println("<meta name='viewport' content='width=device-width, initial-scale=1.0'>");
        out.println("<meta http-equiv='X-UA-Compatible' content='ie=edge'>");
        out.println("<title>导航</title>");

        out.println("<!-- 引入bootstrap框架的样式文件 -->");
        out.println("<link rel='stylesheet' href='"+req.getContextPath()+"/plugins/bootstrap/css/bootstrap.css' type='text/css'>");
        out.println("<link rel='stylesheet' href='"+req.getContextPath()+"/css/nav.css' type='text/css'>");
        out.println("</head>");

        out.println("<body>");
        out.println("<div class='container-fluid'>");
        out.println("<div class='row'>");
        out.println("<nav class='navbar navbar-inverse' role='navigation'>");
        out.println("<div class='container-fluid'>");
        out.println("<div class='navbar-header'>");
        out.println("<a class='navbar-brand' href='#'>手机商城</a>");
        out.println("</div>");
        out.println("<div class='collapse navbar-collapse' id='example-navbar-collapse'>");
        out.println("<ul class='nav navbar-nav navbar-right'>");
        out.println("<li class='active'><a href='#'>首页</a></li>");
        if(null == user){
            out.println("<li><a href='#'>注册</a></li>");
            out.println("<li><a href='"+req.getContextPath()+"/user/loginView'>登录</a></li>");
        }else{
            out.println("<li><a href='#'>"+user.getUsername()+"</a></li>");
            out.println("<li><a href='"+req.getContextPath()+"/user/loginOut'>安全退出</a></li>");
            out.println("<li><img id='headerImg' src='"+req.getContextPath()+"/imgs/tx.png'></li>");
        }

        out.println("</ul>");
        out.println("</div>");
        out.println("</div>");
        out.println("</nav>");
        out.println("</div>");
        out.println("</div>");
        out.println("<!--响应式导航end-->");
        //转发包含..

        //获取模糊匹配的的路径
        String pathInfo = req.getPathInfo();

        switch (pathInfo){
            case "/loginview":
                req.getRequestDispatcher("/user/loginView").include(req,resp);
                break;
            case "/phone/indexView":
                req.getRequestDispatcher("/phone/indexView").include(req,resp);
                break;
//            case "/phone/detail":
//                req.getRequestDispatcher("/phone/detail").include(req,resp);
//                break;

            case "/phone/detailView":
                req.getRequestDispatcher("/phone/detailView").include(req,resp);
                break;
        }

    }

}
